﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;


namespace Housing
{
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class Users
    {
        private int _userId;
        private string _userName;
        private string _password;
        private string _userType;
        private string _userStreet;
        private string _signature;

        public Users() 
        {
        }

        public Users(string userName)
        {
            GetUserByUserName(userName);
        }

        // properties
        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public string UserType
        {
            get { return _userType; }
            set { _userType = value; }
        }

        public string UserStreet
        {
            get { return _userStreet; }
            set { _userStreet = value; }
        }

        public string Signature
        {
            get { return _signature; }
            set { _signature = value; }
        }


        // methods
        protected string connString = SqlHelper.ConnectionString;
        public void GetUserByUserId(int userId)
        {
            string query = "SELECT * FROM Users WHERE User_id="+userId;

            SqlDataReader dr = SqlHelper.ExecuteReader(connString, CommandType.Text, query);

            if (dr.Read())
            {
                UserId = (int)dr[0];
                UserName = Utils.GetString(dr[1]);
                Password = Utils.GetString(dr[2]);
                UserType = Utils.GetString(dr[3]);
                UserStreet = Utils.GetString(dr[4]);
                Signature = Utils.GetString(dr[5]);
            }

            dr.Close();
        }

        public DataSet GetAllUsers(string userid)
        {
            string query = "SELECT * FROM Users WHERE user_id!=" + userid;
            DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.ConnectionString, CommandType.Text, query);
            return ds;
        }

        public bool AddNewUser(Users user)
        {
            string query = "INSERT INTO Users(User_name, User_pwd, User_type, User_street)"+
                            "VALUES('"+user.UserName+"','"+user.Password+"',"+
                            "'"+user.UserType+"','"+user.UserStreet+"')";

            return Convert.ToBoolean(SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query));
        }

        public bool UpdateAUser( Users user )
        {
            string query = "UPDATE Users SET User_name='"+user.UserName+"', "+
                            "User_pwd='"+user.Password+"',User_street='"+user.UserStreet+"', "+
                            "User_type='" + user.UserType + "',user_signature='" + user.Signature + "' WHERE User_id=" + user.UserId;

            return Convert.ToBoolean(SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query));
        }

        public bool DeleteAUser(int userId)
        {
            string query = "DELETE FROM Users WHERE User_id="+userId;

            return Convert.ToBoolean(SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query));
        }

        public void GetUserByUserName(string userName)
        {
            string query = "SELECT * FROM Users WHERE user_name='"+userName+"'";
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString,CommandType.Text,query);
            if (dr.Read())
            {
                UserId = (int)dr[0];
                UserName = Utils.GetString(dr[1]);
                Password = Utils.GetString(dr[2]);
                UserType = Utils.GetString(dr[3]);
                UserStreet = Utils.GetString(dr[4]);
                Signature = Utils.GetString(dr[5]);
            }
            dr.Close();
        }
    }
}

